package cst.a931xmc.yuejian20.Model;

import java.util.List;

import cst.a931xmc.yuejian20.Bean.ShowBean;
import cst.a931xmc.yuejian20.Interface.ItemIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.ShowService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShowModel<T> extends ApiModel implements ItemIface {
    private Retrofit retrofit;
    private ShowService service;

    public ShowModel(){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(ShowService.class);
    }

    @Override
    public void getList(final Listener l) {
        Call<List<ShowBean>> call = service.getShowList();
        call.enqueue(new Callback<List<ShowBean>>() {
            @Override
            public void onResponse(Call<List<ShowBean>> call, Response<List<ShowBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ShowBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void getMyShowList(int user_id, final Listener l) {
        Call<List<ShowBean>> call = service.getMyShowList(user_id);
        call.enqueue(new Callback<List<ShowBean>>() {
            @Override
            public void onResponse(Call<List<ShowBean>> call, Response<List<ShowBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ShowBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getDetail(int show_id, final Listener l) {
        Call<ShowBean> call = service.getShowDetail(show_id);
        call.enqueue(new Callback<ShowBean>() {
            @Override
            public void onResponse(Call<ShowBean> call, Response<ShowBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<ShowBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
}
